function output = eu_spd(pm,r,theta,str,y)

% Computes the expected utility for a SPD protocol

% pm is a 5x1 vector of parameters, pm = (alpha,beta,kappa,delta,gamma),
% r is the crra parameter
% theta contains the monetary payoffs theta = (T,R,P,S)
% str contains the pure strategy
% y contains the beliefs y = (y1,y2,y3) 

alpha = pm(1); beta = pm(2); kappa = pm(3); delta = pm(4); gamma = pm(5);

% transform the strategy number to (x1,x2,x3)
allx = ff2n(3);
x = allx(9-str,:);

if r==1 
   pi = log(theta);
else
   pi = (1/(1-r))*(theta.^(1-r)-1);
end

% player 1 'misbehaves' if defecting and when 2R<T+S
if 2*theta(2) < theta(1)+theta(4)
   q=0;
else
   q=1;
end

eu = (1-kappa)*(x(1)*y(2)*pi(2)+x(1)*(1-y(2))*pi(4)+(1-x(1))*y(3)*pi(1)+(1-x(1))*(1-y(3))*pi(3))...
    +(1-kappa)*(y(1)*x(2)*pi(2)+y(1)*(1-x(2))*pi(1)+(1-y(1))*x(3)*pi(4)+(1-y(1))*(1-x(3))*pi(3))...
    +kappa*(x(1)*x(2)*pi(2)+x(1)*(1-x(2))*pi(4)+(1-x(1))*x(3)*pi(1)+(1-x(1))*(1-x(3))*pi(3))...
    +kappa*(x(1)*x(2)*pi(2)+x(1)*(1-x(2))*pi(1)+(1-x(1))*x(3)*pi(4)+(1-x(1))*(1-x(3))*pi(3))...
    -alpha*(x(1)*(1-y(2))+(1-y(1))*x(3))*(pi(1)-pi(4))...
    -beta*((1-x(1))*y(3)+y(1)*(1-x(2)))*(pi(1)-pi(4))...
    -delta*q*((1-y(1))*x(3)*(pi(1)-pi(4)))...
    -gamma*q*y(1)*(1-x(2))*(pi(1)-pi(4));

output = eu;